package utils 
{
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import utils.BoundariesHelper;
	/**
	 * ...
	 * @author Jeroen Gildemacher
	 */
	public class DirectionHelper 
	{
		public static const DOWN:int = 2;
		public static const UP:int = 0;
		public static const RIGHT:int = 1;
		public static const LEFT:int = 3;
		public static const HERE:int = -1;
		public function DirectionHelper() 
		{
			
		}
		public static function turnRight(direction:int):int {
			return ((direction +1) % 4);
		}
		public static function turnLeft(direction:int):int {
			return (direction -1<0?3:direction-1) ;
		}
		public static function getDx(direction:int):int {
			switch (direction) {
				case 0:
					return 0;
				case 1:
					return 1;
				case 2:
					return 0;
				case 3:
					return -1;
			}
			//trace("error in getDX" + direction);
			return null;
		}
		public static function getDy(direction:int):int {
			switch (direction) {
				case 0:
					return -1;
				case 1:
					return 0;
				case 2:
					return 1;
				case 3:
					return 0;
			}
			//trace("error in getDY" + direction);
			return null;
		}
		public static function getNewpoint(startPoint:Point, direction:int):Point {
			switch (direction) {
				case 0:
					startPoint.y = BoundariesHelper.getY(startPoint.y - 1);
					return startPoint
				case 1:
					startPoint.x = BoundariesHelper.getX(startPoint.x + 1);
					return startPoint
				case 2:
					startPoint.y = BoundariesHelper.getY(startPoint.y + 1);
					return startPoint
				case 3:
					startPoint.x = BoundariesHelper.getX(startPoint.x - 1);
					return startPoint
			}
			trace("error in getNewPoint" + direction);
			return null;
		}
		public static function getBestDirection(from:Point, to:Point): int {
			//trace("from " + from + " to " + to);
			var shortestX:int = 0;
			var shortestY:int = 0;
			//if start = goal
			if (from.x == to.x && from.y==to.y) return HERE;
			//if horizontally equal
			if (from.x == to.x) {
				if (from.y < to.y) {
					return DOWN;
				} else {
					return UP;
				}
			}
			//if vertically equal
			if (from.y == to.y) {
				if (from.x < to.x) {
					return RIGHT;
				} else {
					return LEFT;
				}
			}
			//else: choose vertical or horizontal
			//remember: there is horizontal wraparound
			//---|-------------------|------
			//   f                   t
			if (from.x < to.x) {
				shortestX = to.x - from.x < BoundariesHelper.bounds.right + from.x - to.x?to.x - from.x:BoundariesHelper.bounds.right + from.x - to.x;
			//---|-------------------|------
			//   t                   f
			} else {
				shortestX=from.x-to.x < BoundariesHelper.bounds.right + to.x-from.x?from.x-to.x :BoundariesHelper.bounds.right -from.x + to.x;
			}
			if (Math.abs(from.y - to.y) < shortestX) {
				if (from.y < to.y) {
					return DOWN;
				} else {
					return UP;
				}
			} else {
				if (from.x < to.x) {
					if (to.x - from.x < BoundariesHelper.bounds.right + from.x - to.x) {
						return RIGHT;
					} else {
						return LEFT;
					}
				} else {
					if (from.x - to.x < BoundariesHelper.bounds.right + to.x - from.x) {
						return LEFT;
					} else {
						return RIGHT;
					}
				}
					
			}
			return null;
		}
		public static function oppositeDirection(direction:int):int {
			return (direction + 2) % 4;
		}
		
	}

}